package com.wzt.sysmanage.controller;

import com.wzt.sysmanage.service.IDictionaryItemService;
import com.wzt.sysmanage.domain.DictionaryItem;
import com.wzt.sysmanage.query.DictionaryItemQuery;
import com.wzt.base.util.AjaxResponse;
import com.wzt.base.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Zenith
 */
@RestController
@RequestMapping("/dictionaryItem")
public class DictionaryItemController {
    @Autowired
    public IDictionaryItemService dictionaryItemService;


    /**
     * 保存和修改公用的
     * @param dictionaryItem 传递的实体
     * @return AjaxResponse转换结果
     */
    @PutMapping
    public AjaxResponse addOrUpdate(@RequestBody DictionaryItem dictionaryItem) {
        try {
            if ( dictionaryItem.getId() != null) {
                dictionaryItemService.editOne(dictionaryItem);
            }
            else {
                dictionaryItemService.addOne(dictionaryItem);
            }
            return AjaxResponse.oneResponse();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResponse.oneResponse().setSuccess(false).setMessage("保存对象失败！" + e.getMessage());
        }
    }

    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value = "/{id}")
    public AjaxResponse delete(@PathVariable("id") Long id) {
        try {
                dictionaryItemService.removeOne(id);
            return AjaxResponse.oneResponse();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResponse.oneResponse().setSuccess(false).setMessage("删除对象失败！" + e.getMessage());
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    public AjaxResponse delete(@RequestBody List<Long> ids) {
        try {
            dictionaryItemService.removeMul(ids);
            return AjaxResponse.oneResponse();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResponse.oneResponse().setSuccess(false).setMessage("删除对象失败！" + e.getMessage());
        }
    }

    /**
    * 通过id获取
    * @param id
    * @return 
    */
    @GetMapping("/{id}")
    public AjaxResponse get(@PathVariable("id") Long id) {
        try {
            DictionaryItem dictionaryItem =dictionaryItemService.getOne(id);
            return AjaxResponse.oneResponse().setData(dictionaryItem);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResponse.oneResponse().setSuccess(false).setMessage("获取一个失败！" + e.getMessage());
        }
    }


    /**
    * 获取所有
    * @return
    */
    @GetMapping
    public AjaxResponse list() {

        try {
            List<DictionaryItem> data = dictionaryItemService.getAll();
            return AjaxResponse.oneResponse().setData(data);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResponse.oneResponse().setSuccess(false).setMessage("获取所有失败！" + e.getMessage());
        }
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageResult 分页对象
    */
    @PostMapping
    public AjaxResponse json(@RequestBody DictionaryItemQuery query) {
        try {
            PageResult<DictionaryItem> pageList = dictionaryItemService.getPage(query);
            return AjaxResponse.oneResponse().setData(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResponse.oneResponse().setSuccess(false).setMessage("获取分页数据失败！" + e.getMessage());
        }
    }
}